//
//  BCIndustryScreeningView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/19.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCIndustryScreeningView.h"

//币种
#import "BCIndustryScreeningCoinTypeView.h"
//风险
#import "BCIndustryScreeningRiskLevelView.h"
//收益
#import "BCIndustryScreeningYieldView.h"

//data
#import "BCAllIndustryDataResponse.h"

static NSTimeInterval BCIndustryScreeningViewAnimateTime = 0.25;

static CGFloat BCIndustryScreeningContentViewTopPadding = 20.0f;

static CGFloat BCIndustryScreeningContentViewDoneHeight = 40.0f;

@interface BCIndustryScreeningContentView ()

@property (nonatomic, strong) BCIndustryScreeningCoinTypeView *coinTypeView;
@property (nonatomic, strong) BCIndustryScreeningRiskLevelView *riskLevelView;
@property (nonatomic, strong) BCIndustryScreeningYieldView *yieldView;

/**
 *doneButton
 */
@property (nonatomic, strong) QMUIButton *doneButton;

- (CGFloat)contentHeight;

#pragma mark - data

/**
 *BCAllIndustryData
 */
@property (nonatomic, weak) BCAllIndustryData *screeningData;

/**
 *BCUserWalletCurrDataModel
 */
@property (nonatomic, weak) BCUserWalletCurrDataModel *currModel;


/**
 *BCAllIndustryDataFlowRiskItem
 */
@property (nonatomic, weak) BCAllIndustryDataFlowRiskItem *riskLevel;


/**
 *yieldRange
 */
@property (nonatomic, copy) NSString *yieldRange;



- (void)resetScreeningDataAction;
- (void)contentViewDoneButtonDidClickedAction;

@end

@implementation BCIndustryScreeningContentView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = true;
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    self.coinTypeView = [BCIndustryScreeningCoinTypeView new];
    [self addSubview:self.coinTypeView];
    //
    self.riskLevelView = [BCIndustryScreeningRiskLevelView new];
    [self addSubview:self.riskLevelView];
    //
    self.yieldView = [BCIndustryScreeningYieldView new];
    [self addSubview:self.yieldView];
    
    //
    [self.coinTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(BCIndustryScreeningContentViewTopPadding);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo([self.coinTypeView viewHeight]);
    }];
    [self.riskLevelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.coinTypeView.mas_bottom);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo([self.riskLevelView viewHeight]);
    }];
    [self.yieldView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.riskLevelView.mas_bottom);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo([self.yieldView viewHeight]);
    }];
    //
    self.doneButton = [QMUIButton new];
    [self.doneButton setTitle:BCLocalizedKey(@"general_done") forState:UIControlStateNormal];
    [self.doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.doneButton.backgroundColor = [UIColor darkMainColor];
    [self addSubview:self.doneButton];
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self);
        make.height.mas_equalTo(BCIndustryScreeningContentViewDoneHeight);
    }];
}



- (CGFloat)contentHeight {
    CGFloat height = BCIndustryScreeningContentViewTopPadding;
    if (self.coinTypeView) {
        height += [self.coinTypeView viewHeight];
    }
    if (self.riskLevelView) {
        height += [self.riskLevelView viewHeight];
    }
    if (self.yieldView) {
        height += [self.yieldView viewHeight];
    }
    height += BCIndustryScreeningContentViewDoneHeight;
    return height;
}

- (void)setScreeningData:(BCAllIndustryData *)screeningData {
    _screeningData = screeningData;
    //币种
    self.coinTypeView.title = screeningData.flow.curs;
    
    NSArray *curs = screeningData.curs;
    //
    self.coinTypeView.source = curs;
    //风险
    self.riskLevelView.title = screeningData.flow.risk;
    NSArray *risks = screeningData.flow.risk_item;
    self.riskLevelView.source = risks;
    
    //收益
    self.yieldView.title = screeningData.flow.profit;
    CGFloat yieldMin = screeningData.flow.profit_min/100.0;
    CGFloat yieldMax = screeningData.flow.profit_max/100.0;
    [self.yieldView setMinValue:yieldMin maxValue:yieldMax];

    //layout
    [self.coinTypeView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo([self.coinTypeView viewHeight]);
    }];
    [self.riskLevelView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo([self.riskLevelView viewHeight]);
    }];
    //
    [self layoutIfNeeded];
}


- (void)resetScreeningDataAction {
    //币种
    BCUserWalletCurrDataModel *currModel = self.currModel;//当前选择的币种
    BCUserWalletCurrDataModel *coinType = self.coinTypeView.currModel;//币种视图选择的币种
    coinType.isSelect = false;
    currModel.isSelect = true;
    self.coinTypeView.currModel = currModel;
    [self.coinTypeView.typeCollection reloadData];
}

- (void)contentViewDoneButtonDidClickedAction {
    self.currModel = self.coinTypeView.currModel;
}

@end

@interface BCIndustryScreeningView ()

@property (nonatomic, strong) UIView *overlayBackgroundView;

/**
 *contentView
 */
@property (nonatomic, strong) BCIndustryScreeningContentView *contentView;

@end

@implementation BCIndustryScreeningView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.clipsToBounds = true;
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    self.overlayBackgroundView = [UIView new];
    [self addSubview:self.overlayBackgroundView];
    self.overlayBackgroundView.backgroundColor = RGBHEX(0x000000);
    [self.overlayBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    self.overlayBackgroundView.alpha = 0;
    //
    CGFloat contentHeight = [self.contentView contentHeight];
    self.contentView = [BCIndustryScreeningContentView new];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(contentHeight);
        make.top.mas_equalTo(self).offset(-contentHeight);
    }];
    //
    [self.contentView.doneButton addTarget:self action:@selector(contentViewDoneButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
}



- (void)showScreeningViewComplete:(void (^)(void))complete {
    CGFloat contentHeight = [self.contentView contentHeight];
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(contentHeight);
        make.top.mas_equalTo(self).offset(0);
    }];
    [UIView animateWithDuration:BCIndustryScreeningViewAnimateTime animations:^{
        self.overlayBackgroundView.alpha = 0.14;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (complete) {
            complete();
        }
    }];
    //设置一下数据
}

- (void)hiddenScreeningViewComplete:(void (^)(void))complete {
    //
    [self resetScreeningDataAction];
    //
    CGFloat contentHeight = [self.contentView contentHeight];
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(contentHeight);
        make.top.mas_equalTo(self).offset(-contentHeight);
    }];
    [UIView animateWithDuration:BCIndustryScreeningViewAnimateTime animations:^{
        self.overlayBackgroundView.alpha = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (complete) {
            complete();
        }
    }];
}
/**
 设置筛选数据 在选择后可能咩有点击确定 所以需要重新设置
 */
- (void)resetScreeningDataAction {
    [self.contentView resetScreeningDataAction];
}

#pragma mark - getRequestData
- (NSString *)yieldRange {
    return nil;
}
- (NSString *)curId {
    BCUserWalletCurrDataModel *currModel = self.contentView.currModel;
    if (currModel&&currModel.isSelect) {
        return [NSString stringWithFormat:@"%@",@(currModel.cur_id)];
    }
    return nil;
}
- (NSString *)risk {
    return nil;
}


#pragma mark - contentViewDoneButtonDidClickedAction
- (void)contentViewDoneButtonDidClickedAction {
    [self.contentView contentViewDoneButtonDidClickedAction];
    if (self.delegate && [self.delegate respondsToSelector:@selector(industryScreeningViewDelegateDidClickedDoneButtonAction)]) {
        [self.delegate industryScreeningViewDelegateDidClickedDoneButtonAction];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = touches.anyObject;
    CGPoint touchPoint = [touch locationInView:self];
    if (!CGRectContainsPoint(self.contentView.frame, touchPoint)) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(industryScreeningViewDelegateDidCanceledAction)]) {
            [self.delegate industryScreeningViewDelegateDidCanceledAction];
        }
    }
}


#pragma mark - setData
//设置数据重新加载视图
- (void)setScreeningData:(BCAllIndustryData *)screeningData {
    if (_screeningData) {//设置一次就够了以免数据错乱
        return;
    }
    _screeningData = screeningData;
    //
    self.contentView.screeningData = screeningData;
}

@end
